;Application Include file
;Makes creating applications for the ti83+ calculators a lot easier
;Written by Darth Android (darthandroid@gmail.com) for DASM

#ifdef __TI83P_APPLICATION
  .warn "File already included previously, skipping."
  .end
#endif
#define __TI83P_APPLICATION
#macro "Application.createApp","V:name"
  #module "application"
    #ifdef "appname@application" ;make sure appname@application isn't already defined (indicates Application.createApp() has already been called)
      .error "The application '", appname, "' is already in the process of being built."
    #else
      #if $length(name)>8
        .error "Application name '",name,"' is ",$length(name)-8," character(s) too long."
      #else
        #define "appname",name
        #define "apppage",0
        .org $4000 ;Application start point
        .db $80,$0F,0,0,0,0 ;Application header
        .db $80,$12,1,4
        .db $80,$21,1
        .db $80,$31,1
        .db $80,$48
        .db name ;Application name
        .fill 8-$length(name),' ' ;padding for the app name
        .db $80,$81,0 ;rest of the application header
        .db $80,$90
        .db $03,$26,$09,$04,$04,$06f,$1b,$80
        .db $02,$0d,$40
        .db $a1,$6b,$99,$f6,$59,$bc,$67 
        .db $f5,$85,$9c,$09,$6c,$0f,$b4,$03,$9b,$c9 
        .db $03,$32,$2c,$e0,$03,$20,$e3,$2c,$f4,$2d 
        .db $73,$b4,$27,$c4,$a0,$72,$54,$b9,$ea,$7c 
        .db $3b,$aa,$16,$f6,$77,$83,$7a,$ee,$1a,$d4 
        .db $42,$4c,$6b,$8b,$13,$1f,$bb,$93,$8b,$fc
        .db $19,$1c,$3c,$ec,$4d,$e5,$75 
        .db $80,$7F,0,0,0,0
        .dw 0,0,0,0,0,0,0,0
      #endif ;name length check
    #endif ;second use check
  #endmodule ;"application" module
#endmacro ;"Application.createApp()" macro

#macro "Application.nextPage"
  #module "application"
    #ifndef "appname@application"
      .error "Application.createApp() must be called prior to calling Application.nextPage()"
    #else
      #ifndef "apppage@application"
        .error "Application.createApp() must be called prior to calling Application.nextPage()"
      #else
        #if ($&$0000FFFF)>$8000
          .error "The application '",appname,"', page ",apppage,", went over it's boundary by ",($&$0000FFFF)-$8000," bytes."
        #else
          .block $8000-($&$0000FFFF)
          #define "apppage",$eval("apppage")+1
          .org $4000+apppage*$10000
        #endif ;page size check
      #endif ;apppage@application check
    #endif ;appname@application check
  #endmodule ;"application" module
#endmacro ;"Application.nextPage()" macro

#macro "Application.validate"
  #module "application"
    #ifndef "appname@application"
      .error "Application.createApp() must be called prior to calling Application.validate()"
    #else
      #ifndef "apppage@application"
        .error "Application.createApp() must be called prior to calling Application.validate()"
      #else
        #if ($&$0000FFFF)>$8000
          .error "The application '",appname,"', page ",apppage,", went over it's boundary by ",($&$0000FFFF)-$8000," bytes."
        #else
          .echoln "The application '",appname,"' validated successfully and is ready for signing. It is ",apppage," page(s) in size."
        #endif ;page boundary check
      #endif ;page definition check
    #endif ;application name check
  #endmodule ;"application" module
#endmacro ;"Application.validate()" macro